#!/usr/bin/python
# -- coding:utf8 --
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import app.xingwei.util_log as utils
# import xingwei.util_log as utils


import asyncio
import pickle
import platform
import requests
import lightgbm as lgb
import pandas as pd
import numpy as np
import re
import datetime
from collections import Counter
import unidecode
import json
import time
import random
from xingwei.xx_sms_key_20240604 import *

import warnings
# warnings.filterwarnings('ignore')

if platform.system() != "Windows":
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
else:
    import nest_asyncio
    nest_asyncio.apply()

def get_pickle(file_name, request = None):
    if request:
        pkl = request.app.state.models.get(file_name, None)
    else:
        pkl = None
    
    if pkl is not None:
        pass
    else:
        file_path_name = './model/'+file_name
        pkl = load_data_from_pickle(file_path_name)
    
    return pkl

def get_json_cache(file_name, request = None):

    if request:
        ph_reg = request.app.state.jsons.get(file_name, None)
        # print(ph_reg)
    else:
        ph_reg = None
        
    if ph_reg is not None:
        pass
    else:
        file_path_name = './model/'+file_name
        current_path = os.path.dirname(__file__)
        file_path_name = file_path_name.replace("./", current_path + "/")
        with open(file_path_name, 'r') as f:
            ph_reg = json.load(f)
    
    return ph_reg

def load_data_from_pickle(file_path_name):
    """
    加载pkl格式数据
    :param file_path_name:
    :return:
    """
    current_path = os.path.dirname(__file__)
    file_path_name = file_path_name.replace("./", current_path + "/")
    file_path_name = file_path_name.replace("\\\\", "/")
    with open(file_path_name, "rb") as infile:
        result = pickle.load(infile)
    return result

def p2score(p, pdo=40, base_score=700):
    """
    概率转换为分数的函数
    说明：概率p=坏的概率
    :param p:
    :param pdo:
    :param base_score:
    :return:
    """
    b = pdo / np.log(2)
    a = base_score + b * np.log(1 / 20)
    if p < 0:
        return -99999
    min_p = 1e-9
    max_p = 1 - min_p
    if p >= max_p:
        p = max_p
    elif p <= min_p:
        p = min_p
    score = int(a + b * np.log((1 - p) / (p)))
    if score < 300:
        return 300
    elif score > 900:
        return 900
    else:
        return score

def cal_strdate_to_int(apply_time):  # 13位时间戳
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(s_t)) * 1000


def cal_strdate_to_datetime(apply_time):
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time


def tmp_d(fz, fm):
    if fm == 0 or not fm:
        return -999
    else:
        return round(fz / fm, 4)


def convert_to_str(obj):
    """
    如果遇到 object 就转换为字符串
    """
    if isinstance(obj, dict):
        return {k: convert_to_str(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [convert_to_str(item) for item in obj]
    elif isinstance(obj, object):
        return str(obj)
    else:
        return obj


def cal_regular_simple(x):
    x = str(x).lower()
    x = re.sub("ngayong", "ngayon", x)
    x = re.sub("ngayung", "ngayon", x)
    x = re.sub("due date", "duedate", x)
    x = re.sub("reminder", "remind", x)
    x = re.sub("na po ang", "is", x)
    x = re.sub("po ang", "is", x)
    x = re.sub("napo an", "is", x)
    x = re.sub("ang araw ng", "is", x)
    x = re.sub("reminds", "remind", x)
    x = re.sub("na ang", "is", x)
    x = re.sub("nyo na po", "is", x)
    x = re.sub("na ang", "is", x)
    x = re.sub("paalala", "remind", x)
    x = re.sub("araw ang", "is", x)  # ngayun
    x = re.sub("ngayun", "ngayon", x)  # ngayun

    x = re.sub("paalala", "remind", x)
    return x


def get_new_score_v2(params, request = None):

    start_time = time.time()

    json_data = get_json_cache('ph_reg_20240814.json', request=None)

    package_id_2m = json_data['package_id_2m']
    package_id_1m = json_data['package_id_1m']
    package_id_50w = json_data['package_id_50w']
    package_id_loan = json_data['package_id_loan']
    package_id_48 = json_data['package_id_48']
    package_id_46 = json_data['package_id_46']
    package_id_44 = json_data['package_id_44']
    package_id_42 = json_data['package_id_42']
    sms_org_pattern = json_data['sms_org_pattern']
    bank_org_pattern = json_data['bank_org_pattern']
    sms_org_list = json_data['sms_org_list']
    app_del_key = json_data['app_del_key']
    sms_dele_key = json_data['sms_dele_key']
    sms_verif_key = json_data['sms_verif_key']
    sms_cash_key = json_data['sms_cash_key']
    sms_loan_key = json_data['sms_loan_key']
    sms_repayment_key = json_data['sms_repayment_key']
    sms_expire_key = json_data['sms_expire_key']
    sms_overdue_key = json_data['sms_overdue_key']
    sms_overdue_serious_key = json_data['sms_overdue_serious_key']
    sms_bill_key = json_data['sms_bill_key']
    sms_bill_key = json_data['sms_bill_key']

    current_path = os.path.dirname(__file__)
    file_path_name4 = 'new_v2_20240812.pkl'
    lgbm_pkl = get_pickle(file_path_name4, request=None)

    # 订单
    order_id = params['order_id']
    apply_time = params['apply_time']
    apply_time_int = cal_strdate_to_int(str(apply_time))
    apply_time_datetime = cal_strdate_to_datetime(apply_time)

    # 1 基本模块信息
    # y = Birthday
    y = params['y']
    # x = ApplyDate
    x = params['x']
    # 计算年龄
    y = str(y)[:4]
    x = str(x)[:4]
    age = int(x) - int(y)

    # 教育信息
    Education = params['Education']
    Education = int(Education)

    # 2 APP列表模块的特征集
    # app list dict
    start = time.time()
    app_data = params['app_data']
    app_data = [x for x in app_data if x['appName'] and x['packageName']
                and x['is_system'] == "0" and x['firstInstallTime'] >= "2020-01-01" and x[
                    'lastUpdateTime'] >= "2020-01-01"]

    n1 = len(app_data)
    app_data = [x for x in app_data if not (x['firstInstallTime'] == x['lastUpdateTime']
                                            and (apply_time_int - cal_strdate_to_int(
                x['firstInstallTime'])) / 3600 / 1000 / 24 / 365 >= 1)]
    app_data = [x for x in app_data if
                not re.search(app_del_key.lower(), x['appName'].lower() + x['packageName'].lower())]

    first_install_time = [cal_strdate_to_int(x['firstInstallTime']) for x in app_data]
    last_update_time = [cal_strdate_to_int(x['lastUpdateTime']) for x in app_data]

    in_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in first_install_time]
    up_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in last_update_time]
    all_hours = [(y - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x, y in
                 zip(first_install_time, last_update_time)]

    in_hours_int = [int(x) for x in in_hours]


    """ 计算inday """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['firstInstallTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for
                       x in
                       app_data]
    app_1d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_cash_15d = [x for x in app_15d if re.search(package_id_loan, x)]
    app_cash_15d_cnt = len(app_cash_15d)
    app_cash_90d = [x for x in app_90d if re.search(package_id_loan, x)]
    app_cash_90d_cnt = len(app_cash_90d)
    app_cash_15d_div_90d_inday = tmp_d(app_cash_15d_cnt, app_cash_90d_cnt)
    app_7d_div_90d_inday = tmp_d(app_7d_cnt, app_90d_cnt)
    app_3d_div_60d_inday = tmp_d(app_3d_cnt, app_60d_cnt)
    app_7d_rate_inday = tmp_d(app_7d_cnt, app_cnt)
    app_1d_cnt_inday = len(app_1d)
    app_3d_cnt_inday = len(app_3d)
    app_7d_cnt_inday = len(app_7d)
    app_90d_rate_inday = tmp_d(app_90d_cnt, app_cnt)
    app_3d_rate_inday = tmp_d(app_3d_cnt, app_cnt)
    app_cash_1d = [x for x in app_1d if re.search(package_id_loan, x)]
    app_cash_1d_cnt = len(app_cash_1d)
    app_cash_1d_rate_inday = tmp_d(app_cash_1d_cnt, app_1d_cnt)



    """ 计算 upday """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['lastUpdateTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for x
                       in
                       app_data]
    app_1d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_cash = [x for x in app_name_and_id if re.search(package_id_loan, x)]
    app_cash_cnt = len(app_cash)
    app_cash_rate = tmp_d(app_cash_cnt, app_cnt)
    app_cash_1m_360d = [x for x in app_360d_gg if re.search(package_id_1m, x + "@")]
    app_cash_1m_360d_cnt = len(app_cash_1m_360d)
    app_cash_1m_360d_rate_upday = tmp_d(app_cash_1m_360d_cnt, app_cash_cnt)
    app_cash_1d = [x for x in app_1d if re.search(package_id_loan, x)]
    app_cash_1d_cnt = len(app_cash_1d)
    app_cash_90d = [x for x in app_90d if re.search(package_id_loan, x)]
    app_cash_90d_cnt = len(app_cash_90d)
    app_cash_1d_div_90d_upday = tmp_d(app_cash_1d_cnt, app_cash_90d_cnt)
    app_cash_1m_360d = [x for x in app_360d_gg if re.search(package_id_1m, x + "@")]
    app_cash_1m_360d_cnt_upday = len(app_cash_1m_360d)
    app_cash_3d = [x for x in app_3d if re.search(package_id_loan, x)]
    app_cash_3d_cnt = len(app_cash_3d)
    app_cash_30d = [x for x in app_30d if re.search(package_id_loan, x)]
    app_cash_30d_cnt = len(app_cash_30d)
    app_cash_3d_div_30d_upday = tmp_d(app_cash_3d_cnt, app_cash_30d_cnt)
    app_7d_div_60d_upday = tmp_d(app_7d_cnt, app_60d_cnt)

    app_cash_2m_15d = [x for x in app_15d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_15d_cnt_upday = len(app_cash_2m_15d)

    """ allday """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (cal_strdate_to_int(x['lastUpdateTime']) - cal_strdate_to_int(
            x['firstInstallTime'])) // 3600 / 1000 / 24 + random.random() * 0.0001) for x in
                       app_data]
    app_1d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 1]
    app_3d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 3]
    app_7d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 7]
    app_15d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 15]
    app_30d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 30]
    app_60d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 60]
    app_90d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 90]
    app_180d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 180]
    app_360d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_cash_42_30d = [x for x in app_30d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_30d_cnt = len(app_cash_42_30d)
    app_cash_42_360d = [x for x in app_360d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_360d_cnt = len(app_cash_42_360d)
    app_cash_30d_div_360d_42_allday = tmp_d(app_cash_42_30d_cnt, app_cash_42_360d_cnt)
    app_cash_180d = [x for x in app_180d if re.search(package_id_loan, x)]
    app_cash_180d_cnt = len(app_cash_180d)
    app_cash_180d_rate_allday = tmp_d(app_cash_180d_cnt, app_180d_cnt)
    app_cash_44_7d = [x for x in app_7d_gg if re.search(package_id_44, x + "@")]
    app_cash_44_7d_cnt = len(app_cash_44_7d)
    app_cash_44_30d = [x for x in app_30d_gg if re.search(package_id_44, x + "@")]
    app_cash_44_30d_cnt = len(app_cash_44_30d)
    app_cash_7d_div_30d_44_allday = tmp_d(app_cash_44_7d_cnt, app_cash_44_30d_cnt)
    app_360d_cnt_allday = len(app_360d)

    app_cash_360d = [x for x in app_360d if re.search(package_id_loan, x)]
    app_cash_360d_cnt_allday = len(app_cash_360d)

    app_cash_2m_30d = [x for x in app_30d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_30d_cnt = len(app_cash_2m_30d)
    app_cash_2m_360d = [x for x in app_360d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_360d_cnt = len(app_cash_2m_360d)
    app_cash_30d_div_360d_2m_allday = tmp_d(app_cash_2m_30d_cnt, app_cash_2m_360d_cnt)

    app_cash_46_1d = [x for x in app_1d_gg if re.search(package_id_46, x + "@")]
    app_cash_46_1d_cnt_allday = len(app_cash_46_1d)
    end = time.time()
    # utils.get_logger().info('ApplyNO {} app features time {}'.format(order_id, end-start))

    start = time.time()
    # 3 短信模块特征集
    # 3.1 计算 tfidf
    sms_json = params['sms_json']
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    body = [item['sms_content'].replace('\n', '').lower() + " " + item['contactor_name'].replace('\n', '').lower() for
            item
            in sms_json]


    def cal_wb(x):
        x = str(x)
        x = x.lower()
        x = re.sub("lo@n", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("l0an", "loan", x)
        x = re.sub("udpate", "update", x)
        x = re.sub("verlflcatlon", "verification", x)
        x = re.sub("l\*an", "loan", x)
        x = x.replace('\n', ' ')
        x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
        x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
        x = x.lstrip()
        return x.lower()


    body = [cal_wb(unidecode.unidecode(x.lower())) for x in body]  # 去掉重音符
    send_time = [cal_strdate_to_int(item['send_time']) for item in sms_json]
    day = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in send_time]

    body_1d = [body[day.index(x)] for x in day if x <= 1]
    body_3d = [body[day.index(x)] for x in day if x <= 3]
    body_7d = [body[day.index(x)] for x in day if x <= 7]
    body_15d = [body[day.index(x)] for x in day if x <= 15]
    body_30d = [body[day.index(x)] for x in day if x <= 30]
    body_60d = [body[day.index(x)] for x in day if x <= 60]
    body_90d = [body[day.index(x)] for x in day if x <= 90]
    body_360d = [body[day.index(x)] for x in day if x <= 360]
    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_30d_upto = [x for x in body_30d if re.search('approve|up to|approval|interest|upto', x)]
    body_30d_upto_cnt_tfidf = len(body_30d_upto)
    body_3d_apply = [x for x in body_3d if re.search("application|apply", x.lower())]
    body_3d_apply_cnt = len(body_3d_apply)
    body_7d_apply = [x for x in body_7d if re.search("application|apply", x.lower())]
    body_7d_apply_cnt = len(body_7d_apply)
    body_apply_3d_div_7d_tfidf = tmp_d(body_3d_apply_cnt, body_7d_apply_cnt)
    body_30d_close = [x for x in body_30d if re.search('close', x)]
    body_30d_close_cnt = len(body_30d_close)
    body_30d_close_rate_tfidf = tmp_d(body_30d_close_cnt, body_30d_cnt)
    body_30d_reloan = [x for x in body_30d if re.search("reloan| re loan ", x.lower())]
    body_30d_reloan_cnt_tfidf = len(body_30d_reloan)
    body_legal = [x for x in body if re.search(' legal | law | atty ', x.lower())]
    body_legal_cnt = len(body_legal)
    body_1d_legal = [x for x in body_1d if re.search(' legal | law | atty ', x.lower())]
    body_1d_legal_cnt = len(body_1d_legal)
    body_1d_legal_rate = tmp_d(body_1d_legal_cnt, body_1d_cnt)
    body_1d_legal_div_legal_tfidf = tmp_d(body_1d_legal_cnt, body_legal_cnt)
    body_installment = [x for x in body if re.search('schedule|installment', x)]
    body_installment_cnt_tfidf = len(body_installment)
    body_15d_upto = [x for x in body_15d if re.search('approve|up to|approval|interest|upto', x)]
    body_15d_upto_cnt = len(body_15d_upto)
    body_360d_upto = [x for x in body_360d if re.search('approve|up to|approval|interest|upto', x)]
    body_360d_upto_cnt = len(body_360d_upto)
    body_upto_15d_div_360d_tfidf = tmp_d(body_15d_upto_cnt, body_360d_upto_cnt)
    body_1d_upto = [x for x in body_1d if re.search('approve|up to|approval|interest|upto', x)]
    body_1d_upto_cnt_tfidf = len(body_1d_upto)
    body_90d_https = [x for x in body_90d if re.search("https", x.lower())]
    body_90d_https_cnt_tfidf = len(body_90d_https)
    body_7d_close = [x for x in body_7d if re.search('close', x)]
    body_7d_close_cnt = len(body_7d_close)
    body_7d_close_rate_tfidf = tmp_d(body_7d_close_cnt, body_7d_cnt)
    body_7d_bill = [x for x in body_7d if re.search(" balance | bill | balanse |statement", x.lower())]
    body_7d_bill_cnt_tfidf = len(body_7d_bill)
    body_60d_utang = [x for x in body_60d if re.search("unsettle|utang|debtor|unpaid", x.lower())]
    body_60d_utang_cnt = len(body_60d_utang)
    body_60d_utang_rate = tmp_d(body_60d_utang_cnt, body_60d_cnt)
    body_utang = [x for x in body if re.search("unsettle|utang|debtor|unpaid", x.lower())]
    body_utang_cnt = len(body_utang)
    body_60d_utang_div_utang_tfidf = tmp_d(body_60d_utang_cnt, body_utang_cnt)
    body_3d_reloan = [x for x in body_3d if re.search("reloan| re loan ", x.lower())]
    body_3d_reloan_cnt = len(body_3d_reloan)
    body_7d_reloan = [x for x in body_7d if re.search("reloan| re loan ", x.lower())]
    body_7d_reloan_cnt = len(body_7d_reloan)
    body_reloan_3d_div_7d_tfidf = tmp_d(body_3d_reloan_cnt, body_7d_reloan_cnt)
    body_congrat = [x for x in body if re.search('congrat', x)]
    body_congrat_cnt_tfidf = len(body_congrat)
    body_3d_duedate = [x for x in body_3d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_3d_duedate_cnt = len(body_3d_duedate)
    body_360d_duedate = [x for x in body_360d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_360d_duedate_cnt = len(body_360d_duedate)
    body_duedate_3d_div_360d_tfidf = tmp_d(body_3d_duedate_cnt, body_360d_duedate_cnt)
    body_1d_upto = [x for x in body_1d if re.search('approve|up to|approval|interest|upto', x)]
    body_1d_upto_cnt = len(body_1d_upto)
    body_3d_upto = [x for x in body_3d if re.search('approve|up to|approval|interest|upto', x)]
    body_3d_upto_cnt = len(body_3d_upto)
    body_upto_1d_div_3d_tfidf = tmp_d(body_1d_upto_cnt, body_3d_upto_cnt)
    body_3d_installment = [x for x in body_3d if re.search('schedule|installment', x)]
    body_3d_installment_cnt = len(body_3d_installment)
    body_7d_installment = [x for x in body_7d if re.search('schedule|installment', x)]
    body_7d_installment_cnt = len(body_7d_installment)
    body_installment_3d_div_7d_tfidf = tmp_d(body_3d_installment_cnt, body_7d_installment_cnt)
    body_1d_congrat = [x for x in body_1d if re.search('congrat', x)]
    body_1d_congrat_cnt = len(body_1d_congrat)
    body_3d_congrat = [x for x in body_3d if re.search('congrat', x)]
    body_3d_congrat_cnt = len(body_3d_congrat)
    body_congrat_1d_div_3d_tfidf = tmp_d(body_1d_congrat_cnt, body_3d_congrat_cnt)
    body_15d_discount = [x for x in body_15d if re.search('discount', x)]
    body_15d_discount_cnt = len(body_15d_discount)
    body_90d_discount = [x for x in body_90d if re.search('discount', x)]
    body_90d_discount_cnt = len(body_90d_discount)
    body_discount_15d_div_90d_tfidf = tmp_d(body_15d_discount_cnt, body_90d_discount_cnt)
    body_advance = [x for x in body if re.search("advance", x.lower())]
    body_advance_cnt_tfidf = len(body_advance)
    body_60d_installment = [x for x in body_60d if re.search('schedule|installment', x)]
    body_60d_installment_cnt = len(body_60d_installment)
    body_90d_installment = [x for x in body_90d if re.search('schedule|installment', x)]
    body_90d_installment_cnt = len(body_90d_installment)
    body_installment_60d_div_90d_tfidf = tmp_d(body_60d_installment_cnt, body_90d_installment_cnt)
    body_7d_reloan = [x for x in body_7d if re.search("reloan| re loan ", x.lower())]
    body_7d_reloan_cnt_tfidf = len(body_7d_reloan)
    body_15d_duedate = [x for x in body_15d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_15d_duedate_cnt = len(body_15d_duedate)
    body_90d_duedate = [x for x in body_90d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_90d_duedate_cnt = len(body_90d_duedate)
    body_duedate_15d_div_90d_tfidf = tmp_d(body_15d_duedate_cnt, body_90d_duedate_cnt)
    body_duedate = [x for x in body if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_duedate_cnt_tfidf = len(body_duedate)
    body_90d_withdraw = [x for x in body_90d if re.search("withdraw", x.lower())]
    body_90d_withdraw_cnt = len(body_90d_withdraw)
    body_90d_withdraw_rate = tmp_d(body_90d_withdraw_cnt, body_90d_cnt)
    body_withdraw = [x for x in body if re.search("withdraw", x.lower())]
    body_withdraw_cnt = len(body_withdraw)
    body_90d_withdraw_div_withdraw_tfidf = tmp_d(body_90d_withdraw_cnt, body_withdraw_cnt)
    body_7d_congrat = [x for x in body_7d if re.search('congrat', x)]
    body_7d_congrat_cnt = len(body_7d_congrat)
    body_congrat_1d_div_7d_tfidf = tmp_d(body_1d_congrat_cnt, body_7d_congrat_cnt)
    body_success = [x for x in body if re.search("success", x.lower())]
    body_success_cnt_tfidf = len(body_success)
    body_1d_apply = [x for x in body_1d if re.search("application|apply", x.lower())]
    body_1d_apply_cnt = len(body_1d_apply)
    body_apply_1d_div_3d_tfidf = tmp_d(body_1d_apply_cnt, body_3d_apply_cnt)
    body_7d_collection = [x for x in body_7d if re.search("collection", x.lower())]
    body_7d_collection_cnt = len(body_7d_collection)
    body_360d_collection = [x for x in body_360d if re.search("collection", x.lower())]
    body_360d_collection_cnt = len(body_360d_collection)
    body_collection_7d_div_360d_tfidf = tmp_d(body_7d_collection_cnt, body_360d_collection_cnt)
    body_3d_reloan = [x for x in body_3d if re.search("reloan| re loan ", x.lower())]
    body_3d_reloan_cnt_tfidf = len(body_3d_reloan)
    body_360d_advance = [x for x in body_360d if re.search("advance", x.lower())]
    body_360d_advance_cnt_tfidf = len(body_360d_advance)
    body_15d_close = [x for x in body_15d if re.search('close', x)]
    body_15d_close_cnt = len(body_15d_close)
    body_15d_close_rate_tfidf = tmp_d(body_15d_close_cnt, body_15d_cnt)
    body_30d_pastdue = [x for x in body_30d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_30d_pastdue_cnt_tfidf = len(body_30d_pastdue)
    body_90d_reloan = [x for x in body_90d if re.search("reloan| re loan ", x.lower())]
    body_90d_reloan_cnt = len(body_90d_reloan)
    body_reloan_3d_div_90d_tfidf = tmp_d(body_3d_reloan_cnt, body_90d_reloan_cnt)



    # 3.2 计算org
    sms_topcash_key = 'mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'
    sms_1m_cash_key = 'valleyloan|fastcash|maypera|wowpera|lemonloan|pesohere|zippeso|pesoredee|easypeso|mrcash|peramoo|cashme|moneycat|upeso|mocasa|cashbee|mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'


    def regular_extraction(org_content, x):
        x = str(x).lower()
        matches = re.search(org_content, x)
        y = "-999"
        if matches:
            s = matches.start()
            e = matches.end()
            y = x[s:e]
        return y


    def tq_xw(x):
        x = str(x)
        x = x.strip()
        x = unidecode.unidecode(x.lower())
        pattern = "$(.*?)$|<(.*?)>|【(.*?)】|\[(.*?)\]|\((.*?)\)|\{(.*?)\}|（(.*?)）|(^[^0-9]{0,15}):|(^[^0-9]{0,15})：|\*\*(.*?)\*\*"
        matches = re.search(pattern, x)
        y = "-999"
        if matches:
            s = matches.start()
            e = matches.end()
            y = x[s:e]
            y = re.sub(r'[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜ]', '', y)  # 保留数字英文和空格

            # 新增逻辑
            # 兜底

            y = re.sub("ioan", "loan", y)
            y = re.sub("lon", "loan", y)
            y = re.sub("csh", "cash", y)
            y = re.sub("u0005", "", y)
            y = re.sub("u001b", "", y)
            y = re.sub("cah", "cash", y)

            y = y.strip()

            # 删除结尾的
            if re.search("vip\d+$", y):
                tmp = re.search("vip\d+$", y)
                len_tmp = len(tmp.group(0))
                y = y[:-len_tmp]
            if re.search("ph$", y):
                y = y[:-2]
            if re.search("vip$", y):
                y = y[:-3]
            if re.search("ios$", y):
                y = y[:-3]

            # 删除开头
            if re.search("^support", y):
                y = y[len("support"):]

            if 'bene' in y:
                y = 'bene'
            if "moregold" in y:
                y = "moregold"
            if "moregold" in y:
                y = "moregold"
            if "peramo" in y:
                y = "peramo"
            if "tala" in y:
                y = "tala"
            if "negosyoko" in y:
                y = 'negosyoko'
            if "bpi" in y:
                y = 'bpi'

        return y


    def cal_sms_org_all(sms_org_list, sms_org_pattern, sms_content, contactor_name):
        sms_content_ok = cal_sms_content(sms_content)
        contactor_name = str(contactor_name).lower()
        contactor_name = re.sub(r'[^a-z0-9]', '', contactor_name)
        sms_org = tq_xw(sms_content)
        if len(sms_org) <= 0:
            sms_org = "-999"
        sms_list_org = [x for x in sms_org_list if x == sms_org or x == contactor_name]
        if len(sms_list_org) >= 1:
            y = sms_list_org[0]
        else:
            y = '-999'
        if y == '-999':
            y = regular_extraction(sms_org_pattern, sms_content_ok + " " + contactor_name)
            y = re.sub(' ', '', y)  # 去空格
        return y


    def cal_sms_content(x):
        x = str(x)
        x = x.lower()
        x = re.sub("lo@n", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("l0an", "loan", x)
        x = re.sub("udpate", "update", x)
        x = re.sub("verlflcatlon", "verification", x)
        x = re.sub("l\*an", "loan", x)
        x = x.replace('\n', ' ')
        x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
        x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
        x = x.lstrip()
        return x.lower()


    def cal_boy_type(x, type):
        y = 0
        if re.search(type, x):
            y = 1
        return y
    
    sms_json = params['sms_json']
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    sms_json = [
        {**x, 'sms_org_ok': cal_sms_org_all(sms_org_list, sms_org_pattern, x["sms_content"], x["contactor_name"])} for x
        in sms_json]
    sms_json = [{**x, "sms_org_bank": regular_extraction(bank_org_pattern, x["sms_content"] + x["contactor_name"])} for
                x in sms_json]

    sms_json = [{**x, "sms_content_ok": cal_sms_content(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_verif_key": cal_boy_type(x["sms_content_ok"], sms_verif_key)} for x in sms_json]
    sms_json = [{**x, "sms_cash_key": cal_boy_type(x["sms_content_ok"], sms_cash_key)} for x in sms_json]
    sms_json = [{**x, "sms_loan_key": cal_boy_type(x["sms_content_ok"], sms_loan_key)} for x in sms_json]
    sms_json = [{**x, "sms_repayment_key": cal_boy_type(x["sms_content_ok"], sms_repayment_key)} for x in sms_json]
    sms_json = [{**x, "sms_expire_key": cal_boy_type(x["sms_content_ok"], sms_expire_key)} for x in sms_json]
    sms_json = [{**x, "sms_bill_key": cal_boy_type(x["sms_content_ok"], sms_bill_key)} for x in sms_json]
    sms_json = [{**x, "sms_overdue_serious_key": cal_boy_type(x["sms_content_ok"], sms_overdue_serious_key)} for x in
                sms_json]
    sms_json = [
        {**x, "sms_overdue_key": cal_boy_type(x["sms_content_ok"], sms_overdue_key + "|" + sms_overdue_serious_key)}
        for x in sms_json]
    sms_json = [{**x, "sms_topcash_key": cal_boy_type(x["sms_org_ok"], sms_topcash_key)} for x in sms_json]
    sms_json = [{**x, "sms_1m_cash_key": cal_boy_type(x["sms_org_ok"], sms_1m_cash_key)} for x in sms_json]

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in
                sms_json]

    body = [x for x in sms_json]
    body_1d = [x for x in sms_json if x["day"] <= 1]
    body_3d = [x for x in sms_json if x["day"] <= 3]
    body_7d = [x for x in sms_json if x["day"] <= 7]
    body_15d = [x for x in sms_json if x["day"] <= 15]
    body_30d = [x for x in sms_json if x["day"] <= 30]
    body_60d = [x for x in sms_json if x["day"] <= 60]
    body_90d = [x for x in sms_json if x["day"] <= 90]
    body_360d = [x for x in sms_json if x["day"] <= 360]

    tmp = [x['sms_org_ok'] for x in body if x['sms_org_ok'] != "-999"]
    body_cnt = len(set(tmp))
    tmp = [x['sms_org_ok'] for x in body_1d if x['sms_org_ok'] != "-999"]
    body_1d_cnt = len(set(tmp))
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_3d if x['sms_org_ok'] != "-999"]
    body_3d_cnt = len(set(tmp))
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_7d if x['sms_org_ok'] != "-999"]
    body_7d_cnt = len(set(tmp))
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_15d if x['sms_org_ok'] != "-999"]
    body_15d_cnt = len(set(tmp))
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_30d if x['sms_org_ok'] != "-999"]
    body_30d_cnt = len(set(tmp))
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_60d if x['sms_org_ok'] != "-999"]
    body_60d_cnt = len(set(tmp))
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_90d if x['sms_org_ok'] != "-999"]
    body_90d_cnt = len(set(tmp))
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_360d if x['sms_org_ok'] != "-999"]
    body_360d_cnt = len(set(tmp))
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_7d_verif = [x['sms_org_ok'] for x in sms_json if
                     x["sms_verif_key"] == 1 and x["day"] <= 7 and x['sms_org_ok'] != "-999"]
    body_7d_verif = set(body_7d_verif)
    body_7d_verif_cnt = len(body_7d_verif)
    body_7d_verif_rate_org = tmp_d(body_7d_verif_cnt, body_7d_cnt)
    body_3d_verif = [x['sms_org_ok'] for x in sms_json if
                     x["sms_verif_key"] == 1 and x["day"] <= 3 and x['sms_org_ok'] != "-999"]
    body_3d_verif = set(body_3d_verif)
    body_3d_verif_cnt = len(body_3d_verif)
    body_3d_verif_rate_org = tmp_d(body_3d_verif_cnt, body_3d_cnt)
    body_30d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 30 and x['sms_org_ok'] != "-999"]
    body_30d_verif = set(body_30d_verif)
    body_30d_verif_cnt = len(body_30d_verif)
    body_30d_verif_rate_org = tmp_d(body_30d_verif_cnt, body_30d_cnt)
    body_15d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 15 and x['sms_org_ok'] != "-999"]
    body_15d_verif = set(body_15d_verif)
    body_15d_verif_cnt = len(body_15d_verif)
    body_15d_verif_rate_org = tmp_d(body_15d_verif_cnt, body_15d_cnt)
    body_overdue_topcash = [x['sms_org_ok'] for x in sms_json if
                            x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_topcash_key'] == 1
                            and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x[
                                'sms_org_ok'] != "-999"]
    body_overdue_topcash = set(body_overdue_topcash)
    body_overdue_topcash_cnt = len(body_overdue_topcash)
    body_3d_overdue_topcash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_topcash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 3 and
                               x['sms_org_ok'] != "-999"]
    body_3d_overdue_topcash = set(body_3d_overdue_topcash)
    body_3d_overdue_topcash_cnt = len(body_3d_overdue_topcash)
    body_3d_overdue_topcash_rate = tmp_d(body_3d_overdue_topcash_cnt, body_3d_cnt)
    body_3d_overdue_topcash_div_overdue_topcash_org = tmp_d(body_3d_overdue_topcash_cnt, body_overdue_topcash_cnt)
    body_1d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 1 and x[
                           'sms_org_ok'] != "-999"]
    body_1d_overdue = set(body_1d_overdue)
    body_1d_overdue_cnt = len(body_1d_overdue)
    body_30d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                        and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 30 and x[
                            'sms_org_ok'] != "-999"]
    body_30d_overdue = set(body_30d_overdue)
    body_30d_overdue_cnt = len(body_30d_overdue)
    body_overdue_1d_div_30d_org = tmp_d(body_1d_overdue_cnt, body_30d_overdue_cnt)
    body_verif = [x['sms_org_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x['sms_org_ok'] != "-999"]
    body_verif = set(body_verif)
    body_verif_cnt = len(body_verif)
    body_verif_rate_org = tmp_d(body_verif_cnt, body_cnt)
    body_60d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 60 and x['sms_org_ok'] != "-999"]
    body_60d_verif = set(body_60d_verif)
    body_60d_verif_cnt = len(body_60d_verif)
    body_60d_verif_rate_org = tmp_d(body_60d_verif_cnt, body_60d_cnt)
    body_3d_settle = [x['sms_org_ok'] for x in sms_json if
                      x["sms_repayment_key"] == 1 and x["day"] <= 3 and x['sms_org_ok'] != "-999"]
    body_3d_settle = set(body_3d_settle)
    body_3d_settle_cnt = len(body_3d_settle)
    body_15d_settle = [x['sms_org_ok'] for x in sms_json if
                       x["sms_repayment_key"] == 1 and x["day"] <= 15 and x['sms_org_ok'] != "-999"]
    body_15d_settle = set(body_15d_settle)
    body_15d_settle_cnt = len(body_15d_settle)
    body_settle_3d_div_15d_org = tmp_d(body_3d_settle_cnt, body_15d_settle_cnt)


    # 3.3 计算 base sms
    sms_json = params['sms_json']
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    # 新逻辑
    sms_json = [
        {**x, 'sms_org_ok': cal_sms_org_all(sms_org_list, sms_org_pattern, x["sms_content"], x["contactor_name"])} for x
        in sms_json]
    sms_json = [{**x, "sms_org_bank": regular_extraction(bank_org_pattern, x["sms_content"] + x["contactor_name"])} for
                x in sms_json]

    sms_json = [{**x, "sms_content_ok": cal_sms_content(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_verif_key": cal_boy_type(x["sms_content_ok"], sms_verif_key)} for x in sms_json]
    sms_json = [{**x, "sms_cash_key": cal_boy_type(x["sms_content_ok"], sms_cash_key)} for x in sms_json]
    sms_json = [{**x, "sms_loan_key": cal_boy_type(x["sms_content_ok"], sms_loan_key)} for x in sms_json]
    sms_json = [{**x, "sms_repayment_key": cal_boy_type(x["sms_content_ok"], sms_repayment_key)} for x in sms_json]
    sms_json = [{**x, "sms_expire_key": cal_boy_type(cal_regular_simple(x["sms_content_ok"]),
                                                     cal_regular_simple(sms_expire_key))} for x in sms_json]
    sms_json = [{**x, "sms_bill_key": cal_boy_type(x["sms_content_ok"], sms_bill_key)} for x in sms_json]
    sms_json = [{**x, "sms_overdue_serious_key": cal_boy_type(x["sms_content_ok"], sms_overdue_serious_key)} for x in
                sms_json]
    sms_json = [
        {**x, "sms_overdue_key": cal_boy_type(x["sms_content_ok"], sms_overdue_key + "|" + sms_overdue_serious_key)} for
        x in sms_json]
    sms_json = [{**x, "sms_topcash_key": cal_boy_type(x["sms_org_ok"], sms_topcash_key)} for x in sms_json]
    sms_json = [{**x, "sms_1m_cash_key": cal_boy_type(x["sms_org_ok"], sms_1m_cash_key)} for x in sms_json]

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in
                sms_json]

    body = [x for x in sms_json]
    body_1d = [x for x in sms_json if x["day"] <= 1]
    body_3d = [x for x in sms_json if x["day"] <= 3]
    body_7d = [x for x in sms_json if x["day"] <= 7]
    body_15d = [x for x in sms_json if x["day"] <= 15]
    body_30d = [x for x in sms_json if x["day"] <= 30]
    body_60d = [x for x in sms_json if x["day"] <= 60]
    body_90d = [x for x in sms_json if x["day"] <= 90]
    body_360d = [x for x in sms_json if x["day"] <= 360]

    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_1d_loan = [x['sms_content_ok'] for x in sms_json if x["sms_loan_key"] == 1 and x["day"] <= 1]
    body_1d_loan_cnt = len(body_1d_loan)
    body_7d_loan = [x['sms_content_ok'] for x in sms_json if x["sms_loan_key"] == 1 and x["day"] <= 7]
    body_7d_loan_cnt = len(body_7d_loan)
    body_loan_1d_div_7d = tmp_d(body_1d_loan_cnt, body_7d_loan_cnt)

    body_15d_loan = [x['sms_content_ok'] for x in sms_json if x["sms_loan_key"] == 1 and x["day"] <= 15]
    body_15d_loan_cnt = len(body_15d_loan)
    body_loan_7d_div_15d = tmp_d(body_7d_loan_cnt, body_15d_loan_cnt)


    body_1d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 1]
    body_1d_overdue_cnt = len(body_1d_overdue)
    body_1d_overdue_rate = tmp_d(body_1d_overdue_cnt, body_1d_cnt)
    body_90d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                        and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 90]
    body_90d_overdue_cnt = len(body_90d_overdue)
    body_90d_overdue_rate = tmp_d(body_90d_overdue_cnt, body_90d_cnt)
    body_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                    and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0]
    body_overdue_cnt = len(body_overdue)
    body_overdue_rate = tmp_d(body_overdue_cnt, body_cnt)
    body_3d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 3]
    body_3d_overdue_cnt = len(body_3d_overdue)
    body_3d_overdue_rate = tmp_d(body_3d_overdue_cnt, body_3d_cnt)
    body_360d_overdue_serious = [x['sms_content_ok'] for x in sms_json if
                                 x["sms_overdue_serious_key"] == 1 and x["day"] <= 360]
    body_360d_overdue_serious_cnt = len(body_360d_overdue_serious)
    body_settle_1m_cash = [x['sms_content_ok'] for x in sms_json if
                           x["sms_repayment_key"] == 1 and x["sms_1m_cash_key"] == 1]
    body_settle_1m_cash_cnt = len(body_settle_1m_cash)
    body_3d_expire = [x['sms_content_ok'] for x in sms_json if
                      x["sms_expire_key"] == 1 and x["day"] <= 3]
    body_3d_expire_cnt = len(body_3d_expire)
    body_3d_expire_rate = tmp_d(body_3d_expire_cnt, body_3d_cnt)
    body_expire = [x['sms_content_ok'] for x in sms_json if
                   x["sms_expire_key"] == 1]
    body_expire_cnt = len(body_expire)
    body_3d_expire_div_expire = tmp_d(body_3d_expire_cnt, body_expire_cnt)
    body_360d_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                         x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1 and x[
                                             "day"] <= 360]
    body_360d_overdue_serious_1m_cash_cnt = len(body_360d_overdue_serious_1m_cash)
    body_expire_topcash = [x['sms_content_ok'] for x in sms_json if
                           x["sms_expire_key"] == 1 and x["sms_topcash_key"] == 1]
    body_expire_topcash_cnt = len(body_expire_topcash)
    body_1d_overdue_serious = [x['sms_content_ok'] for x in sms_json if
                               x["sms_overdue_serious_key"] == 1 and x["day"] <= 1]
    body_1d_overdue_serious_cnt = len(body_1d_overdue_serious)
    body_1d_overdue_serious_rate = tmp_d(body_1d_overdue_serious_cnt, body_1d_cnt)
    body_overdue_serious = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_serious_key"] == 1]
    body_overdue_serious_cnt = len(body_overdue_serious)
    body_1d_overdue_serious_div_overdue_serious = tmp_d(body_1d_overdue_serious_cnt, body_overdue_serious_cnt)
    body_3d_loan_1m_cash = [x['sms_content_ok'] for x in sms_json if
                            x["sms_loan_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 3]
    body_3d_loan_1m_cash_cnt = len(body_3d_loan_1m_cash)
    body_7d_loan_1m_cash = [x['sms_content_ok'] for x in sms_json if
                            x["sms_loan_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 7]
    body_7d_loan_1m_cash_cnt = len(body_7d_loan_1m_cash)
    body_loan_1m_cash_3d_div_7d = tmp_d(body_3d_loan_1m_cash_cnt, body_7d_loan_1m_cash_cnt)
    body_15d_cash_verif = [x['sms_content_ok'] for x in sms_json if
                           x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 15]
    body_15d_cash_verif_cnt = len(body_15d_cash_verif)
    body_60d_settle = [x['sms_content_ok'] for x in sms_json if x["sms_repayment_key"] == 1 and x["day"] <= 60]
    body_60d_settle_cnt = len(body_60d_settle)
    body_360d_settle = [x['sms_content_ok'] for x in sms_json if x["sms_repayment_key"] == 1 and x["day"] <= 360]
    body_360d_settle_cnt = len(body_360d_settle)
    body_settle_60d_div_360d = tmp_d(body_60d_settle_cnt, body_360d_settle_cnt)
    body_360d_bill = [x['sms_content_ok'] for x in sms_json if x["sms_bill_key"] == 1 and x["day"] <= 360]
    body_360d_bill_cnt = len(body_360d_bill)
    body_settle = [x['sms_content_ok'] for x in sms_json if x["sms_repayment_key"] == 1]
    body_settle_cnt = len(body_settle)
    body_7d_overdue_serious = [x['sms_content_ok'] for x in sms_json if
                               x["sms_overdue_serious_key"] == 1 and x["day"] <= 7]
    body_7d_overdue_serious_cnt = len(body_7d_overdue_serious)
    body_overdue_serious_1d_div_7d = tmp_d(body_1d_overdue_serious_cnt, body_7d_overdue_serious_cnt)
    body_15d_loan_1m_cash = [x['sms_content_ok'] for x in sms_json if
                             x["sms_loan_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 15]
    body_15d_loan_1m_cash_cnt = len(body_15d_loan_1m_cash)
    body_360d_loan_1m_cash = [x['sms_content_ok'] for x in sms_json if
                              x["sms_loan_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 360]
    body_360d_loan_1m_cash_cnt = len(body_360d_loan_1m_cash)
    body_loan_1m_cash_15d_div_360d = tmp_d(body_15d_loan_1m_cash_cnt, body_360d_loan_1m_cash_cnt)
    body_60d_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["day"] <= 60]
    body_60d_verif_cnt = len(body_60d_verif)
    tmp = [x['sms_content_ok'] for x in body if x['sms_org_bank'] != "-999"]
    body_bank_cnt = len(tmp)
    body_overdue_1m_cash = [x['sms_content_ok'] for x in sms_json if
                            x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_1m_cash_key'] == 1
                            and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0]
    body_overdue_1m_cash_cnt = len(body_overdue_1m_cash)



    body_360d_cash_verif = [x['sms_content_ok'] for x in sms_json if
                            x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 360]
    body_360d_cash_verif_cnt = len(body_360d_cash_verif)
    body_360d_cash_verif_rate = tmp_d(body_360d_cash_verif_cnt, body_360d_cnt)
    body_cash_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1]
    body_cash_verif_cnt = len(body_cash_verif)
    body_360d_cash_verif_div_cash_verif = tmp_d(body_360d_cash_verif_cnt, body_cash_verif_cnt)

    body_3d_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                       x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1 and x[
                                           "day"] <= 3]
    body_3d_overdue_serious_1m_cash_cnt = len(body_3d_overdue_serious_1m_cash)
    body_3d_overdue_serious_1m_cash_rate = tmp_d(body_3d_overdue_serious_1m_cash_cnt, body_3d_cnt)
    body_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                    x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1]
    body_overdue_serious_1m_cash_cnt = len(body_overdue_serious_1m_cash)
    body_3d_overdue_serious_1m_cash_div_overdue_serious_1m_cash = tmp_d(body_3d_overdue_serious_1m_cash_cnt,
                                                                        body_overdue_serious_1m_cash_cnt)
    
    end = time.time()
    # utils.get_logger().info('ApplyNO {} sms features time {}'.format(order_id, end-start))

    df_tmp = pd.DataFrame({

        "body_1d_overdue_rate": body_1d_overdue_rate,
        "body_7d_verif_rate_org": body_7d_verif_rate_org,
        "app_cash_1m_360d_rate_upday": app_cash_1m_360d_rate_upday,
        "age": age,
        "body_3d_verif_rate_org": body_3d_verif_rate_org,
        "app_cash_15d_div_90d_inday": app_cash_15d_div_90d_inday,
        "body_30d_verif_rate_org": body_30d_verif_rate_org,
        "body_90d_overdue_rate": body_90d_overdue_rate,
        "body_overdue_rate": body_overdue_rate,
        "body_3d_overdue_rate": body_3d_overdue_rate,
        "body_15d_verif_rate_org": body_15d_verif_rate_org,
        "body_30d_upto_cnt_tfidf": body_30d_upto_cnt_tfidf,
        "Education": Education,
        "body_360d_overdue_serious_cnt": body_360d_overdue_serious_cnt,
        "app_cash_1d_div_90d_upday": app_cash_1d_div_90d_upday,
        "body_apply_3d_div_7d_tfidf": body_apply_3d_div_7d_tfidf,
        "body_settle_1m_cash_cnt": body_settle_1m_cash_cnt,
        "body_30d_close_rate_tfidf": body_30d_close_rate_tfidf,
        "body_30d_reloan_cnt_tfidf": body_30d_reloan_cnt_tfidf,
        "app_cash_30d_div_360d_42_allday": app_cash_30d_div_360d_42_allday,
        "body_3d_expire_div_expire": body_3d_expire_div_expire,
        "body_360d_overdue_serious_1m_cash_cnt": body_360d_overdue_serious_1m_cash_cnt,
        "body_1d_legal_div_legal_tfidf": body_1d_legal_div_legal_tfidf,
        "body_installment_cnt_tfidf": body_installment_cnt_tfidf,
        "body_expire_topcash_cnt": body_expire_topcash_cnt,
        "app_7d_div_90d_inday": app_7d_div_90d_inday,
        "body_upto_15d_div_360d_tfidf": body_upto_15d_div_360d_tfidf,
        "app_cash_1m_360d_cnt_upday": app_cash_1m_360d_cnt_upday,
        "body_1d_upto_cnt_tfidf": body_1d_upto_cnt_tfidf,
        "app_3d_div_60d_inday": app_3d_div_60d_inday,
        "body_1d_overdue_serious_div_overdue_serious": body_1d_overdue_serious_div_overdue_serious,
        "body_90d_https_cnt_tfidf": body_90d_https_cnt_tfidf,
        "app_7d_rate_inday": app_7d_rate_inday,
        "body_7d_close_rate_tfidf": body_7d_close_rate_tfidf,
        "app_cash_180d_rate_allday": app_cash_180d_rate_allday,
        "body_7d_bill_cnt_tfidf": body_7d_bill_cnt_tfidf,
        "body_1d_cnt": body_1d_cnt,
        "body_60d_utang_div_utang_tfidf": body_60d_utang_div_utang_tfidf,
        "body_3d_overdue_topcash_div_overdue_topcash_org": body_3d_overdue_topcash_div_overdue_topcash_org,
        "body_overdue_1d_div_30d_org": body_overdue_1d_div_30d_org,
        "body_reloan_3d_div_7d_tfidf": body_reloan_3d_div_7d_tfidf,
        "body_congrat_cnt_tfidf": body_congrat_cnt_tfidf,
        "body_duedate_3d_div_360d_tfidf": body_duedate_3d_div_360d_tfidf,
        "body_loan_1m_cash_3d_div_7d": body_loan_1m_cash_3d_div_7d,
        "app_cash_7d_div_30d_44_allday": app_cash_7d_div_30d_44_allday,
        "app_360d_cnt_allday": app_360d_cnt_allday,
        "app_cash_360d_cnt_allday": app_cash_360d_cnt_allday,
        "app_1d_cnt_inday": app_1d_cnt_inday,
        "body_upto_1d_div_3d_tfidf": body_upto_1d_div_3d_tfidf,
        "body_installment_3d_div_7d_tfidf": body_installment_3d_div_7d_tfidf,
        "body_cnt": body_cnt,
        "body_congrat_1d_div_3d_tfidf": body_congrat_1d_div_3d_tfidf,
        "app_3d_cnt_inday": app_3d_cnt_inday,
        "body_15d_cash_verif_cnt": body_15d_cash_verif_cnt,
        "body_verif_rate_org": body_verif_rate_org,
        "body_settle_60d_div_360d": body_settle_60d_div_360d,
        "body_expire_cnt": body_expire_cnt,
        "body_360d_bill_cnt": body_360d_bill_cnt,
        "app_7d_cnt_inday": app_7d_cnt_inday,
        "body_settle_cnt": body_settle_cnt,
        "app_90d_rate_inday": app_90d_rate_inday,
        "body_overdue_serious_1d_div_7d": body_overdue_serious_1d_div_7d,
        "body_60d_verif_rate_org": body_60d_verif_rate_org,
        "body_discount_15d_div_90d_tfidf": body_discount_15d_div_90d_tfidf,
        "app_cash_3d_div_30d_upday": app_cash_3d_div_30d_upday,
        "body_settle_3d_div_15d_org": body_settle_3d_div_15d_org,
        "body_advance_cnt_tfidf": body_advance_cnt_tfidf,
        "body_installment_60d_div_90d_tfidf": body_installment_60d_div_90d_tfidf,
        "app_7d_div_60d_upday": app_7d_div_60d_upday,
        "body_loan_1m_cash_15d_div_360d": body_loan_1m_cash_15d_div_360d,
        "app_cash_30d_div_360d_2m_allday": app_cash_30d_div_360d_2m_allday,
        "body_7d_reloan_cnt_tfidf": body_7d_reloan_cnt_tfidf,
        "body_duedate_15d_div_90d_tfidf": body_duedate_15d_div_90d_tfidf,
        "body_60d_verif_cnt": body_60d_verif_cnt,
        "body_duedate_cnt_tfidf": body_duedate_cnt_tfidf,
        "body_90d_withdraw_div_withdraw_tfidf": body_90d_withdraw_div_withdraw_tfidf,
        "app_cash_46_1d_cnt_allday": app_cash_46_1d_cnt_allday,
        "body_congrat_1d_div_7d_tfidf": body_congrat_1d_div_7d_tfidf,
        "body_success_cnt_tfidf": body_success_cnt_tfidf,
        "app_cash_2m_15d_cnt_upday": app_cash_2m_15d_cnt_upday,
        "app_3d_rate_inday": app_3d_rate_inday,
        "body_apply_1d_div_3d_tfidf": body_apply_1d_div_3d_tfidf,
        "body_bank_cnt": body_bank_cnt,
        "body_collection_7d_div_360d_tfidf": body_collection_7d_div_360d_tfidf,
        "body_overdue_1m_cash_cnt": body_overdue_1m_cash_cnt,
        "body_3d_reloan_cnt_tfidf": body_3d_reloan_cnt_tfidf,
        "body_360d_advance_cnt_tfidf": body_360d_advance_cnt_tfidf,
        "app_cash_1d_rate_inday": app_cash_1d_rate_inday,
        "body_loan_1d_div_7d": body_loan_1d_div_7d,
        "body_360d_cash_verif_div_cash_verif": body_360d_cash_verif_div_cash_verif,
        "body_15d_close_rate_tfidf": body_15d_close_rate_tfidf,
        "body_loan_7d_div_15d": body_loan_7d_div_15d,
        "body_3d_overdue_serious_1m_cash_div_overdue_serious_1m_cash": body_3d_overdue_serious_1m_cash_div_overdue_serious_1m_cash,
        "body_30d_pastdue_cnt_tfidf": body_30d_pastdue_cnt_tfidf,
        "body_reloan_3d_div_90d_tfidf": body_reloan_3d_div_90d_tfidf,

    }, index=[1])

    var = ['body_7d_verif_rate_org',
    'body_1d_overdue_rate',
    'app_cash_1m_360d_rate_upday',
    'app_cash_15d_div_90d_inday',
    'body_3d_verif_rate_org',
    'body_overdue_rate',
    'age',
    'body_15d_verif_rate_org',
    'body_30d_upto_cnt_tfidf',
    'body_3d_overdue_rate',
    'body_verif_rate_org',
    'body_90d_overdue_rate',
    'Education',
    'body_installment_cnt_tfidf',
    'body_1d_legal_div_legal_tfidf',
    'app_cash_1m_360d_cnt_upday',
    'body_7d_close_rate_tfidf',
    'app_cash_1d_div_90d_upday',
    'body_360d_overdue_serious_cnt',
    'body_30d_verif_rate_org',
    'body_30d_reloan_cnt_tfidf',
    'app_3d_cnt_inday',
    'app_cash_180d_rate_allday',
    'body_360d_bill_cnt',
    'body_settle_60d_div_360d',
    'body_90d_https_cnt_tfidf',
    'body_overdue_1d_div_30d_org',
    'body_1d_upto_cnt_tfidf',
    'app_360d_cnt_allday',
    'body_7d_bill_cnt_tfidf',
    'body_7d_reloan_cnt_tfidf',
    'body_3d_expire_div_expire',
    'app_7d_div_90d_inday',
    'app_cash_3d_div_30d_upday',
    'body_overdue_serious_1d_div_7d',
    'body_settle_cnt',
    'app_cash_46_1d_cnt_allday',
    'body_congrat_cnt_tfidf',
    'body_30d_close_rate_tfidf',
    'body_installment_60d_div_90d_tfidf',
    'body_duedate_cnt_tfidf',
    'body_90d_withdraw_div_withdraw_tfidf',
    'body_expire_cnt',
    'body_settle_1m_cash_cnt',
    'body_3d_reloan_cnt_tfidf',
    'body_cnt',
    'body_60d_verif_rate_org',
    'body_360d_overdue_serious_1m_cash_cnt',
    'body_loan_1m_cash_3d_div_7d',
    'app_cash_360d_cnt_allday',
    'body_3d_overdue_topcash_div_overdue_topcash_org',
    'body_apply_1d_div_3d_tfidf',
    'body_reloan_3d_div_7d_tfidf',
    'body_bank_cnt',
    'body_1d_cnt',
    'body_3d_overdue_serious_1m_cash_div_overdue_serious_1m_cash',
    'body_60d_utang_div_utang_tfidf',
    'body_advance_cnt_tfidf',
    'app_cash_1d_rate_inday',
    'app_cash_7d_div_30d_44_allday',
    'body_duedate_15d_div_90d_tfidf',
    'body_overdue_1m_cash_cnt',
    'body_settle_3d_div_15d_org',
    'body_loan_1m_cash_15d_div_360d',
    'body_loan_1d_div_7d',
    'body_reloan_3d_div_90d_tfidf',
    'body_congrat_1d_div_3d_tfidf',
    'body_360d_cash_verif_div_cash_verif',
    'app_cash_30d_div_360d_2m_allday',
    'body_upto_15d_div_360d_tfidf',
    'body_15d_close_rate_tfidf',
    'body_duedate_3d_div_360d_tfidf',
    'app_7d_cnt_inday',
    'body_30d_pastdue_cnt_tfidf',
    'body_installment_3d_div_7d_tfidf',
    'body_15d_cash_verif_cnt',
    'body_congrat_1d_div_7d_tfidf',
    'body_expire_topcash_cnt',
    'app_cash_30d_div_360d_42_allday',
    'body_60d_verif_cnt',
    'app_cash_2m_15d_cnt_upday',
    'app_3d_div_60d_inday',
    'body_upto_1d_div_3d_tfidf',
    'app_3d_rate_inday',
    'body_apply_3d_div_7d_tfidf',
    'app_90d_rate_inday',
    'app_1d_cnt_inday',
    'body_360d_advance_cnt_tfidf',
    'body_discount_15d_div_90d_tfidf',
    'body_success_cnt_tfidf',
    'app_7d_div_60d_upday',
    'body_loan_7d_div_15d',
    'body_1d_overdue_serious_div_overdue_serious',
    'app_7d_rate_inday',
    'body_collection_7d_div_360d_tfidf']

    pred01 = lgbm_pkl.predict(df_tmp[var])
    pred01 = pred01[0]

    XWWhitListScoreV2 = p2score(pred01)
    # print(order_id, pred01, XWWhitListScoreV2)

    end_time = time.time()
    print('耗费时间：', end_time - start_time)
    dict_tmp = df_tmp[var].to_dict('records')[0]
    dict_tmp['XWWhitListScoreV2'] = XWWhitListScoreV2

    return dict_tmp


def send_post_request(url, params):
    # 发送POST请求
    response = requests.post(url, data=params)
    
    # 检查请求是否成功
    if response.status_code == 200:
        # 解析JSON响应
        result = response.json()
        order_id = result.get("ApplyNO")
        apply_time = result.get("ApplyDate")
        app_data = result.get("AppList")
        sms_json = result.get("SmsList")
        y = result.get("Birthday")
        x = result.get("ApplyDate")
        marriage = result.get("Maritalstatus")
        sex = result.get("Sex")
        
        Education = result.get("Education")
        
        params = {
            'order_id': order_id,
            'apply_time': apply_time,
            'app_data': app_data,
            'sms_json': sms_json,
            'y': y,
            'x': x,
            'marriage': marriage,
            'sex': sex,
            'Education': Education
        }
        return params
    else:
        print(f"Request failed with status code {response.status_code}")
        return None

# 测试样例
if __name__ == '__main__':
    url = "http://polo.philippines1.top/test/getRiskApiTestData"  # 替换为实际API的URL
    
    # val_df = pd.read_excel('./old_v1_oot_模型分_20240817.xlsx')
    # val_df['order_ids_1'] = val_df['order_ids'].str.replace('s','')

    # for ApplyNO in val_df.head(10)['order_ids_1'].tolist():
    for ApplyNO in ['172569352510639789']:
        
            params = {"ApplyNO": ApplyNO, "ApiType": "XingWeiRisk"} 
            # 获取技术侧准备的数据集 
            test_params = send_post_request(url, params)
            print(test_params)
            # 获取线上计算的新客模型评分
            result = get_new_score_v2(test_params, request=None)
            print(result)
            # gfNewScoreV1, df_tmp, order_id = cal_api_new_v1(test_params)
            # val_df_1 = val_df[val_df['order_ids'].str.contains(order_id)]
            # score_val = val_df_1['score'].tolist()[0]
            # if np.abs(gfNewScoreV1 - score_val) <= 0.1:
            #     print('订单ID', order_id, '无误', '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
            # else:
            #     print('订单ID', order_id, '有偏差: ', np.abs(gfNewScoreV1 - score_val), '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
            #     for var in df_tmp.columns.tolist():
            #         online_var_value = df_tmp[var].tolist()[0]
            #         offline_var_value = val_df_1[var].tolist()[0]
            #         if np.abs(online_var_value - offline_var_value) > 0.1:
            #             print('入模变量：', var, '线上计算值：', online_var_value, '线下计算值：', offline_var_value)

            # print("*"*100)
        # except Exception as e:
        #     print(f"订单ID {ApplyNO} 发生异常错误发生: {e}")
        #     print("*"*100)



    






